package com.pdwl.cashoutpay.controller;

import com.pdwl.cashoutpay.entity.Merchant;
import com.pdwl.cashoutpay.entity.Trade;
import com.pdwl.cashoutpay.entity.res.BaseEntity;
import com.pdwl.cashoutpay.entity.res.MerchantRes;
import com.pdwl.cashoutpay.entity.res.TradeRes;
import com.pdwl.cashoutpay.service.MerchantRegisterService;
import com.pdwl.cashoutpay.service.OrderService;
import com.pdwl.cashoutpay.util.EntityParse;
import com.pdwl.cashoutpay.util.HttpUtil;
import com.pdwl.cashoutpay.util.LogUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("callBack")
public class CallBack {

    @Value("${merchantNotifyUrl}")
    private String merchantNotifyUrl;//商户注册回调地址

    @Autowired
    private MerchantRegisterService merchantRegisterService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("lifeMerchant")
    public void responseRegister(HttpServletRequest request, HttpServletResponse response) throws Exception {
        LogUtils.logInfo("进入create回调方法！");
        Map<String, String> map = EntityParse.httpServletRequestToMap(request);

        BaseEntity baseEntity = new BaseEntity();
        MerchantRes merchantRes = new MerchantRes();
        String url = null;

        String error_code = map.get("error_code");
        String error_message = map.get("error_message");
        if ("0".equals(error_code)) {
            String return_value = map.get("return_value");
            JSONObject json = JSONObject.fromObject(return_value);
            String subMerchantCode = json.getString("subMerchantCode");
            String outMerchantCode = json.getString("outMerchantCode");
            Merchant entity = new Merchant();
            entity.setOutmerchantcode(outMerchantCode);
            entity.setSubmerchantcode(subMerchantCode);
            Merchant merchant = merchantRegisterService.update(entity);

            url = merchant.getRemark();//下游回调地址
            merchantRes = merchantRegisterService.queryByMerchCode(merchant.getMerchcode());
            baseEntity.setError_code("1");
        } else {
            String return_value = map.get("return_value");
            JSONObject json = JSONObject.fromObject(return_value);
            String outMerchantCode = json.getString("outMerchantCode");
            Merchant merchant = merchantRegisterService.queryByOutMerchCode(outMerchantCode);
            merchantRes = merchantRegisterService.queryByOutMerchCode1(outMerchantCode);
            url = merchant.getRemark();
            baseEntity.setError_code("20004");
        }
        LogUtils.logInfo(url);
        baseEntity.setError_message(error_message);
        baseEntity.setData(merchantRes);
        JSONObject merchantResJson = JSONObject.fromObject(baseEntity);//将java对象转换为json对象
        String res = HttpUtil.httpRequest(url, "POST", merchantResJson.toString());
        LogUtils.logInfo("res");
    }

//    @RequestMapping("costMail")
    public void costMail(HttpServletRequest alipayRequest, HttpServletResponse response) {
        LogUtils.logInfo("进入costMail回调方法！");
        Map<String, String> map = EntityParse.httpServletRequestToMap(alipayRequest);
        LogUtils.logInfo("进入costMail回调方法,上游得到的信息：" + map.toString());
        Trade newTrade = new Trade();
        String ourOrderId = map.get("orderno");
        newTrade.setOurorderid(ourOrderId);
        String status = map.get("status");
        String StatusDesc = map.get("StatusDesc");
        if ("Y".equals(status)) {
            newTrade.setStatus("1");
        } else {
            newTrade.setStatus("00");
        }

        Trade trade = orderService.findByOurOrderId(ourOrderId);

        TradeRes tradeRes = new TradeRes();
        tradeRes.setOrderid(trade.getOrderid());
        tradeRes.setOurorderid(trade.getOurorderid());
        tradeRes.setAmount(trade.getAmount());
        tradeRes.setNotifyurl(trade.getNotifyurl());
        tradeRes.setCriditno(trade.getCriditno());
        tradeRes.setBankphoneno(trade.getBankphoneno());
        tradeRes.setStatus(trade.getStatus());
        tradeRes.setMessage(trade.getMessage());

        BaseEntity baseEntity=new BaseEntity();
        baseEntity.setError_code("1");
        baseEntity.setError_message(StatusDesc);
        baseEntity.setData(tradeRes);

        String notifyUrl = trade.getNotifyurl();

        JSONObject json = JSONObject.fromObject(baseEntity);//将java对象转换为json对象

        LogUtils.logInfo("进入costMail回调方法,发送到商户的信息:" + json.toString());
        String res = HttpUtil.httpRequest(notifyUrl, "POST", json.toString());
    }
}
